package com.gold.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import com.gold.entity.AdminUser;
import com.gold.entity.Card;
import com.gold.entity.Province;
import com.gold.myexception.Myexception;
import com.gold.service.CardService;
import com.gold.service.ProvinceService;
import com.gold.util.Myutil;

@Controller
public class CardController {

	@Autowired
	private CardService cs;
	@Autowired
	private ProvinceService ps;

	@RequestMapping("getAllCardByPage")
	public String getAllCardByPage(String nowPageString, ModelMap mm, HttpServletRequest request) {
		AdminUser oldau = (AdminUser) request.getSession().getAttribute("adminLoginUser");
		if (oldau == null) {
			return "redirect:gold/index.jsp";
		}
		Map<String, String> map = new HashMap<String, String>();
		map.put("nowPageString", nowPageString);
		List<Card> cardList = cs.selectAllCardByPage(map);
		List<Province> provinceList = ps.selectAllProvince();
		mm.addAttribute("cardList", cardList);
		mm.addAttribute("provinceList", provinceList);
		mm.addAttribute("nowPage", map.get("nowPageString"));
		return "/gold/card/card.jsp";
	}

	@RequestMapping("toAddCardJsp")
	public String toAddCardJsp(ModelMap mm) {
		List<Province> provinceList = ps.selectAllProvince();
		mm.addAttribute("provinceList", provinceList);
		return "/gold/card/addCard.jsp";
	}

	@RequestMapping("insertCard")
	public String insertCard(String cardCountString, String[] cardProvIdString, String cardAmountString, String cardStartTime, String cardEndTime, ModelMap mm) {
		if (Myutil.checkStringNull(cardAmountString, cardCountString, cardEndTime, cardStartTime)) {
			if (Myutil.checkNumberIsNumber(cardCountString, cardAmountString)) {
				if (cardProvIdString != null && cardProvIdString.length > 0) {
					Map<String, String> map = new HashMap<String, String>();
					map.put("cardCountString", cardCountString);
					map.put("cardAmountString", cardAmountString);
					map.put("cardStartTime", cardStartTime);
					map.put("cardEndTime", cardEndTime);
					try {
						cs.insertCard(map, cardProvIdString);
					} catch (Myexception e) {
						mm.addAttribute("msg", e.getMessage());
					}
				} else {
					mm.addAttribute("msg", "请选择省份");
				}
			} else {
				mm.addAttribute("msg", "必须输入整数");
			}
		} else {
			mm.addAttribute("msg", "空数据");
		}
		return "getAllCardByPage.do";
	}

	@RequestMapping("getCardByLike")
	public String getCardByLike(String cardNumber, String cardStartTime, String cardEndTime, String cardProvId, ModelMap mm) {
		Map<String, String> map = new HashMap<String, String>();
		System.out.println("cardNumber" + cardNumber);
		System.out.println("cardEndTime" + cardEndTime);
		System.out.println("cardProvId" + cardProvId);
		System.out.println("cardStartTime" + cardStartTime);
		map.put("cardNumber", cardNumber);
		map.put("cardEndTime", cardEndTime);
		map.put("cardProvId", cardProvId);
		map.put("cardStartTime", cardStartTime);
		List<Card> cardList = cs.selectCardByLike(map);
		List<Province> provinceList = ps.selectAllProvince();
		mm.addAttribute("cardList", cardList);
		mm.addAttribute("provinceList", provinceList);
		return "/gold/card/card.jsp";
	}

	@RequestMapping("exportCard")
	public ResponseEntity<byte[]> exportCard(String[] arr, HttpServletResponse response) {
		HttpHeaders headers = new HttpHeaders();
		headers.setContentDispositionFormData("attachment", "exportCard.txt");
		headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
		String str = "";
		for (int i = 0; i < arr.length; i++) {
			int cardId = Integer.parseInt(arr[i]);
			str += cs.selectCardByCardId(cardId) + "\n";
		}
		byte[] bt = str.getBytes();
		return new ResponseEntity<byte[]>(bt, headers, HttpStatus.OK);
	}

}
